package com.briup.app02.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.briup.app02.bean.Category;
import com.briup.app02.bean.CategoryExample;
import com.briup.app02.bean.Product;
import com.briup.app02.bean.ProductCategory;
import com.briup.app02.bean.ProductCategoryExample;
import com.briup.app02.dao.CategoryMapper;
import com.briup.app02.dao.ProductCategoryMapper;
import com.briup.app02.dao.extend.ProductCategoryExtendMapper;
import com.briup.app02.service.ICategoryService;
import com.briup.app02.service.IProductCategoryService;
import com.briup.app02.utils.CustomerException;


@Service
public class ProductCategoryServiceImpl implements IProductCategoryService {
	@Autowired
	private ProductCategoryMapper productCategoryMapper;
	
	@Autowired
	private ProductCategoryExtendMapper productCatrgoryExtendMapper;
	
	@Override
	public void saveOrUpdate(ProductCategory productCategory) throws CustomerException{
		if(productCategory.getId()!=null) {
			productCategoryMapper.updateByPrimaryKey(productCategory);
		}else {
			ProductCategoryExample example=new ProductCategoryExample();
			example.createCriteria().andNameEqualTo(productCategory.getName());
			List<ProductCategory>list=productCategoryMapper.selectByExample(example);
			if(list.size()>0) {
				throw new CustomerException("同名的产品分类已经存在");
			}
			productCategoryMapper.insert(productCategory);
		}
	}
	
	@Override
	public List<ProductCategory>query(String name){
		ProductCategoryExample example=new ProductCategoryExample();
		if(name!=null) {
			example.createCriteria().andNameLike("%"+name+"%");
		}
		List<ProductCategory>list=productCategoryMapper.selectByExample(example);
		return list;
	}
	
	@Override
	public void deleteById(long id) throws CustomerException{
		ProductCategory ProductCategory=productCategoryMapper.selectByPrimaryKey(id);
		if(ProductCategory==null) {
			throw new CustomerException("要删除的产品分类信息不存在");
		}
		productCategoryMapper.deleteByPrimaryKey(id);
	}

	@Override
	public List<ProductCategory>pageQuery(int page, int pageSize, String name){
		int pageIndex=(page-1)*pageSize;
		List<ProductCategory>productCategory=productCatrgoryExtendMapper.pageQuery(pageIndex, pageSize, name);
		return productCategory;
	}
	
}	
	/*@Override
	public List<Category> findAll() {
		// 模板查询
		CategoryExample example = new CategoryExample();
		return categoryMapper.selectByExample(example);
	}

	@Override
	public void saveOrUpdate(Category category) {
		if(category.getId() == null) {
			categoryMapper.insert(category);
		} else {
			categoryMapper.updateByPrimaryKey(category);
		}
	}

	@Override
	public Category findById(long id) {
		return categoryMapper.selectByPrimaryKey(id);
	}

	@Override
	public void deleteById(long id) {
		categoryMapper.deleteByPrimaryKey(id);
	}

	@Override
	public List<Category> query(String name) {
		// TODO Auto-generated method stub
		return null;
	}

}*/
